/* 陵园列表 */
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Link } from 'react-router'
import { Button, Spin, Form, Input, Table, DatePicker, Modal, Select,message } from 'antd'
import City from '../../components/city.js'
import RelatedStaff from '../../components/relatedStaff.js'
/* 引用接口文档路径 */
import {
    cemeteryListSelect,cemeteryListInfo,deleteCemeteryList
} from 'actions/companyManage'

import '../../style/userControl.less'

import ModalAddCemeteryList from './addCemeteryListModal.js';


const FormItem = Form.Item
@Form.create({})
//abc
@connect(
  (state, props) => ({
    config: state.config,
    userInfoList: state.userInfoList,
  })
)
export default class app extends Component {
  constructor(props) {
    super(props)
    this.state = {
      list:"",//基础数据列表
      modalList:"",//列表内容详情
      addModalFlg:false,//新增陵园弹窗
      addModalType:"",
      provinceid:"",//陵园地域ID
      relatedStaffModal:false,//关联员工弹窗
      relatedStaffId:"",//关联员工-公司ID
      showModalFlg:false,
    }
    this.keyword={"cemeteryName":"","regionId":""};//搜索关键词
    this.currentNumber = 1;//当前页面
    this.pagesize = 10;//每页条数
  }
  componentDidMount() {
    this.selectList();
  }
  
  selectFn(){//点击搜索按钮
    this.keyword = {
      "cemeteryName":this.props.form.getFieldValue('cemeteryName'),
      "regionId":this.state.provinceid,
    };
    this.currentNumber=1;
    this.selectList();
  }
  selectList(pageNumber){//查询列表
    const datajson = Object.assign({"start":pageNumber||1,"end":this.pagesize}, this.keyword);
    this.currentNumber = pageNumber||1;
    this.props.dispatch(cemeteryListSelect(
      datajson,
      (respose) => {
        this.setState({
          list: respose,
        })
    }))
  }
  
  // 城市选择框选中的值
  getStateprovince(provinceid){
    this.setState({
      provinceid:provinceid
    })
  }

  addReport(type,id){//新增陵园
    //编辑的情况  带数据去组件
    if(type == 2){
        this.props.dispatch(cemeteryListInfo({"cemeteryId":id},
        (respose) => {
            this.setState({
            addModalFlg: true,
            modalList:respose,
            addModalType:type,
          })
        }))
      }else{//新增的情况
        this.setState({
          addModalFlg: true,
          modalList:{},
          addModalType:type,
        })
    }

  }

  addModalCancel(){//关闭新增陵园弹窗
    this.setState({
        addModalFlg: false,
    })
  }

  bindStaff(id){
    //打开关联员工弹窗
    this.setState({
      relatedStaffModal:true,
      relatedStaffId:id
    })
  }
  relatedStaffSuccess(){
    //关联员工弹窗成功回调
    this.setState({
      relatedStaffModal:false,
      relatedStaffId:""
    },()=>{
      this.selectList();
    })
  }
  relatedStaffCancel(){
    //关联员工弹窗取消回调
    this.setState({
      relatedStaffModal:false,
      relatedStaffId:""
    })
  }

  deleteList(id){
    Modal.confirm({
        content:"确定要删除该条信息吗？",
        onOk:()=>{
          this.props.dispatch(deleteCemeteryList(
            {"cemeteryId":id},
            (respose) => {
              var length = this.state.list.respData.count ? this.state.list.respData.count : 0;
              if(Math.ceil(length/this.pagesize) == this.currentNumber && length%this.pagesize == 1 && this.currentNumber != 1){
                this.currentNumber = this.currentNumber - 1;
              }
              message.success(respose.respMsg);
              this.selectList();
          }))
        },
        onCancel:()=>{
        }
    })
  }

  showModal(id){//查看详情
    this.props.dispatch(cemeteryListInfo({"cemeteryId":id},
      (respose) => {
        this.setState({
        showModalFlg: true,
        modalList:respose,
      })
    }))
  }

  showModalCancel(){//关闭查看详情
    this.setState({
      showModalFlg: false,    
    },()=>{
      this.setState({
        modalList:"",
      })
    })
  }


  columns() {//表格标题
    return [
      {
        title: '陵园编号',
        key: 'cemeteryCode',
        dataIndex: 'cemeteryCode',
        width: '10%',
      },

      {
        title: '陵园图标',
        dataIndex: 'cemeteryLogo',
        key: 'cemeteryLogo',
        width: '10%',
        render: (text, recordId, index) => <img style={{width:"80px"}} src={text}/>,
      },
      {
        title: '陵园名称',
        dataIndex: 'cemeteryName',
        key: 'cemeteryName',
        width: '10%',
      },
      {
        title: '陵园地域',
        dataIndex: 'region',
        key: 'region',
        width: '10%',
      },
      {
        title: '陵园电话',
        dataIndex: 'cemeteryMobile',
        key: 'cemeteryMobile',
        width: '10%',
      },
      {
        title: '详细地址',
        dataIndex: 'cemeteryAddress',
        key: 'cemeteryAddress',
        width: '10%',
      },
      {
        title: '操作',
        width: '10%',
        render: (text, recordId, index) => 
        <div>
          <Button type="primary" size="small" onClick={this.showModal.bind(this,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>查看</Button>
          <Button key="seeinfo" type="primary" size="small" onClick={this.addReport.bind(this,2,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>编辑</Button>          
          <Button type="primary" size="small" onClick={this.bindStaff.bind(this,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>关联</Button>
          <Button key="addcourse" type="primary" size="small" onClick={this.deleteList.bind(this,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>删除</Button>          
        </div>,
        
      },
    ]
  }
  


  render () {
    const {form } = this.props
    const { getFieldDecorator } = form    
    const respData = this.state.list.respData ? this.state.list.respData : [];//刷新页面调用接口数据赋值
    const modalList = this.state.modalList.respData ? this.state.modalList.respData :{};
    const dateFormat = 'YYYY/MM/DD';
    return (
      <div className="page">
        {/* 搜索条件筛选块 */}
        <div className="searcDiv">
          <Form onSubmit={this.selectFn.bind(this)} layout="inline">
            <FormItem label="陵园地域">
              {
                getFieldDecorator('regionId', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <City province={this.state.provinceid} stateprovince={this.getStateprovince.bind(this)}/>
                )
              }  
            </FormItem>
            <FormItem label="陵园名称">
              {
                getFieldDecorator('cemeteryName', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <Input placeholder="请输入关键字" size="default" style={{ width: '200px' }} />
                )
              }  
            </FormItem>
            <Button type="primary" onClick={this.selectFn.bind(this)}>搜索</Button>
            <Button type="primary" onClick={this.addReport.bind(this,1)}>新增</Button>
            {/* <Button type="primary" onClick={this.addReport.bind(this)}>导入</Button> */}
          </Form>
        </div>
        {/*数据渲染列表*/}
        <Spin spinning={false}>
          <Table
            rowKey="id" 
            columns={this.columns()}//表格标题列
            dataSource={respData.resultData}
            pagination={{ total: respData.count, current:this.currentNumber,pageSize: this.pagesize, onChange: this.selectList.bind(this)}}
            scroll={{ y: true }}
          />
        </Spin>
        {/* 查看详情 */}
        {/* 信息展示弹窗 */}
        <Modal
          width={"80%"}
          visible={this.state.showModalFlg}
          title="陵园详情"
          onCancel={this.showModalCancel.bind(this)}
          footer={
            <Button key="submit" type="primary"  onClick={this.showModalCancel.bind(this)}>关闭</Button>
          }
        >
          <ul className="joinBlackListModal">
            <li>
              <span className="title">陵园编号:</span><span className="content">{modalList.cemeteryCode}</span>
            </li>
            <li>
              <span className="title">陵园图标:</span><span className="content"><img style={{width:'80px'}} src={modalList.cemeteryLogo?modalList.cemeteryLogo:require('../../images/baseImg.png')}/></span>
            </li>
            <li>
              <span className="title">陵园封面:</span><span className="content"><img style={{width:'180px'}} src={modalList.cemeteryCover?modalList.cemeteryCover:require('../../images/baseImg.png')}/></span>
            </li>
            <li>
              <span className="title">陵园名称:</span><span className="content">{modalList.cemeteryName}</span>
            </li>
            <li>
              <span className="title">陵园地址:</span><span className="content">{modalList.cemeteryAddress}</span>
            </li>
            <li>
              <span className="title">陵园电话:</span><span className="content">{modalList.cemeteryMobile}</span>
            </li>
            <li>
              <span className="title">陵园说明:</span><span className="content" dangerouslySetInnerHTML={{__html: modalList.introduce}}></span>
            </li>
            <li>
              <span className="title">关联员工:</span><span className="content">{modalList.employee}</span>
            </li>
          </ul>
        </Modal>
        {/* 新增课程 和 编辑课程 */}
        {
          this.state.addModalFlg?
          <ModalAddCemeteryList 
            type={this.state.addModalType}
            ModalFlg={this.state.addModalFlg} 
            ModalCancel={this.addModalCancel.bind(this)} 
            callback={this.selectList.bind(this)}
            content={this.state.modalList.respData ? this.state.modalList.respData : {}}
          />
        :""}
        {/* 关联员工组件调用 */}
        {
          this.state.relatedStaffModal?
          <RelatedStaff 
            type={1}
            visible={this.state.relatedStaffModal} 
            successModal={this.relatedStaffSuccess.bind(this)}
            closeModal={this.relatedStaffCancel.bind(this)}
            companyId={this.state.relatedStaffId}
          />
          :""
        }
      </div>
    )
  }
}
